package com.gqh.weather.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gqh.weather.domain.dto.MyPage;
import com.gqh.weather.domain.dto.PageRequest;
import com.gqh.weather.domain.dto.SchoolCreateRequest;
import com.gqh.weather.domain.dto.SchoolUpdateRequest;
import com.gqh.weather.domain.entity.School;
import com.gqh.weather.exception.Result;
import com.gqh.weather.service.SchoolService;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;

/**
 * 单位管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/pingjia/zh/v1/school")
@Validated
public class SchoolController {

    @Autowired
    private SchoolService schoolService;

    /**
     * 单位详情查询
     *
     * @param id 单位ID
     * @return 单位详情
     */
    @GetMapping("/info")
    public Result<School> getSchoolInfo(@RequestParam @NotNull Integer id) {
        log.info("获取单位详情，ID: {}", id);
        return schoolService.getSchoolInfo(id);
    }

    /**
     * 创建单位
     *
     * @param request   创建请求
     * @return 创建结果
     */
    @PostMapping("/create")
    public Result<Void> createSchool(@Valid SchoolCreateRequest request) {
        log.info("创建单位，名称: {}, 创建者: {}", request.getName());
        return schoolService.createSchool(request);
    }

    /**
     * 更新单位信息
     *
     * @return 更新结果
     */
    @PostMapping("/update")
    public Result<Void> updateSchool(@Valid SchoolUpdateRequest request) {
        log.info("更新单位信息，ID: {}", request.getId());
        return schoolService.updateSchool(request);
    }

    /**
     * 删除单位
     *
     * @param id 单位ID
     * @return 删除结果
     */
    @PostMapping("/delete")
    public Result<Void> deleteSchool(@RequestParam @NotNull Integer id) {
        log.info("删除单位，ID: {}", id);
        return schoolService.deleteSchool(id);
    }

    /**
     * 单位列表查询
     *
     * @param pageRequest 分页参数
     * @param citycode    城市码（权限过滤）
     * @return 查询结果
     */
    @GetMapping("/list")
    public Result<MyPage<School>> getSchoolList(@Valid PageRequest<School> pageRequest,
            @RequestParam(required = false) String citycode) {
        log.info("查询单位列表，页码: {}, 数量: {}, 关键字: {}, 城市码: {}", 
                pageRequest.getPage(), pageRequest.getCount(), pageRequest.getKeyword(), citycode);
        return schoolService.getSchoolList(pageRequest, citycode);
    }

    /**
     * 所有单位列表
     *
     * @param citycode 城市码（权限过滤）
     * @return 所有单位列表
     */
    @GetMapping("/list_all")
    public Result<List<School>> getAllSchoolList(@RequestParam(required = false) String citycode) {
        log.info("获取所有单位列表，城市码: {}", citycode);
        return schoolService.getAllSchoolList(citycode);
    }

    /**
     * 导出单位账号
     *
     * @param citycode 城市码
     * @return 导出结果
     */
    @GetMapping("/export")
    public Result<List<School>> exportSchoolAccounts(@RequestParam(required = false) String citycode) {
        log.info("导出单位账号，城市码: {}", citycode);
        return schoolService.exportSchoolAccounts(citycode);
    }

    /**
     * 启用/禁用单位
     *
     * @param id   单位ID
     * @param flag 状态标志：0启用 1禁用
     * @return 操作结果
     */
    @PostMapping("/enable")
    public Result<Void> enableSchool(@RequestParam @NotNull Integer id,
            @RequestParam @NotNull Integer flag) {
        log.info("启用/禁用单位，ID: {}, flag: {}", id, flag);
        return schoolService.enableSchool(id, flag);
    }
}